#include "log.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

static int log_level = LOG_INFO;

void _log_print(int level, const char *file, int line, const char *log, ...)
{
    if (level > log_level)
        return;

    va_list ap;
    switch (level) {
    case LOG_DEBUG:
    case LOG_INFO:
    case LOG_WARN:
        va_start(ap, log);
        vfprintf(stderr, log, ap);
        va_end(ap);
        break;
    case LOG_ERROR:
        fprintf(stderr, "[ERROR] %s +%d: ", file, line);
        va_start(ap, log);
        vfprintf(stderr, log, ap);
        va_end(ap);
        break;
    case LOG_FATAL:
        fprintf(stderr, "[FATAL] %s +%d: ", file, line);
        va_start(ap, log);
        vfprintf(stderr, log, ap);
        va_end(ap);
        exit(1);
        break;
    default:
        break;
    }
}

void set_log_level(int level)
{
    log_level = level;
}
